<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$packageid = (int) get('package');
$version = $DB->escape(get('version'));
$prevVersion = $DB->escape(get('previous_version'));

$error = false;

$vData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}vms_versions WHERE package_id = '{$packageid}' AND version = '{$version}'");
if(!is_array($vData) || count($vData)==0){
    $error = true;
    JFX::addError('Invalid Version');
}

$pVData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}vms_versions WHERE package_id = '{$packageid}' AND version = '{$prevVersion}'");
if(!is_array($pVData) || count($pVData)==0){
    $error = true;
    JFX::addError('Invalid Previous Version');
}

if(!$error){
    $curFolders = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}vms_files WHERE package_id = '{$packageid}' AND version_id = '{$vData['id']}' AND file_type = 0 ORDER BY filename ASC");
    $curFiles = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}vms_files WHERE package_id = '{$packageid}' AND version_id = '{$vData['id']}' AND file_type = 1 ORDER BY filename ASC");

    $prevFolders = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}vms_files WHERE package_id = '{$packageid}' AND version_id = '{$pVData['id']}' AND file_type = 0 ORDER BY filename ASC");
    $prevFiles = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}vms_files WHERE package_id = '{$packageid}' AND version_id = '{$pVData['id']}' AND file_type = 1 ORDER BY filename ASC");

    $SMARTY->assign('curFolders', $curFolders);
    $SMARTY->assign('curFiles', $curFiles);

    echo '<h1>Compare Versions</h1>';
    echo '<p>Comparing file differences when upgrading from '.$prevVersion.' to '.$version.'</p>';

    echo VMS_Thickbox_CompareVersions_DiffFiles($curFolders, $curFiles, $prevFolders, $prevFiles, $version, $prevVersion);

}


























function VMS_Thickbox_CompareVersions_DiffFiles($curFolders, $curFiles, $prevFolders, $prevFiles, $version, $prevVersion){
    $vmsObject = JFX::registry('JFX_Module_Vms');
    $CONFIG = JFX::registry('config');

    $table = '<table id="compare-versions"><tbody>';
    $allFiles = array();
    $allFolders = array();
    foreach($curFiles as $k=>$v){
        $allFiles[$v['filename']] = $v['filename'];
    }
    foreach($prevFiles as $k=>$v){
        $allFiles[$v['filename']] = $v['filename'];
    }
    foreach($curFolders as $k=>$v){
        $allFolders[$v['filename']] = $v['filename'];
    }
    foreach($prevFolders as $k=>$v){
        $allFolders[$v['filename']] = $v['filename'];
    }

    

    $allFolders = array_regenerate_keys($allFolders);

    $tableContent = '';

    foreach($allFolders as $rowNum => $filename){
        $fData = VMS_Thickbox_CompareVersions_FindIt('filename', $filename, $curFolders);
        if($fData !== NULL){
            // folder exists in current version
            $fData = VMS_Thickbox_CompareVersions_FindIt('filename', $filename, $prevFolders);
            if($fData !== NULL){
                // folder exists in previous version
                $actionIcon = '<img src="'.$vmsObject->imageUrl.'/no-change.png" alt="No Change" />';
                continue;
            }else{
                // folder does not exist in previous version
                $actionIcon = '<img src="'.$vmsObject->imageUrl.'/create-folder.png" alt="Create Folder" />';
            }

            $tableContent .= '<tr><td>'.$filename.'</td><td>'.$actionIcon.'</td></tr>';
        }else{
            // folder not existing in current version
            $fData = VMS_Thickbox_CompareVersions_FindIt('filename', $filename, $prevFolders);
            if($fData !== NULL){
                // folder exists in previous version
                $actionIcon = '<img src="'.$vmsObject->imageUrl.'/delete.png" alt="Delete" />';
            }

            $tableContent .= '<tr><td>'.$filename.'</td><td>'.$actionIcon.'</td></tr>';
        }
    }




    foreach($allFiles as $rowNum => $filename){
        $fData = VMS_Thickbox_CompareVersions_FindIt('filename', $filename, $curFiles);
        if($fData !== NULL && $fData['update_action']!=3){
            // file exists in current version
            $pFData = VMS_Thickbox_CompareVersions_FindIt('filename', $filename, $prevFiles);
            if($pFData !== NULL){
                // file exists in previous version
                if(file_exists($vmsObject->dirname.'/files/'.$fData['id'].'.vmsf') && file_exists($vmsObject->dirname.'/files/'.$fData['id'].'.vmsf')){
                    if(md5_file($vmsObject->dirname.'/files/'.$fData['id'].'.vmsf') !==
                    md5_file($vmsObject->dirname.'/files/'.$pFData['id'].'.vmsf')){
                        //file changed
                        $actionIcon = '<img src="'.$CONFIG->imageUrl.'/edit.png" alt="Update file" /><br />';
                        $actionIcon .= '<a target="_blank" 
                            href="'.$vmsObject->adminUrl.'/download-file/?file='.$pFData['id'].'">'.$prevVersion.'</a>
                            <a target="_blank"
                            href="'.$vmsObject->adminUrl.'/download-file/?file='.$fData['id'].'">'.$version.'</a>
                            ';
                    }else{
                        // file not changed
                        $actionIcon = '<img src="'.$CONFIG->imageUrl.'/no-change.png" alt="No Change" />';
                        continue;
                    }
                }else{
                    // error, one file contents is missing!
                    $actionIcon = '<img src="'.$CONFIG->imageUrl.'/create-file.png" alt="Create File" />';
                }
            }else{
                
                // file does not exist in previous version
                $actionIcon = '<img src="'.$CONFIG->imageUrl.'/create-file.png" alt="Create File" />';
            }

            $tableContent .= '<tr><td>'.$filename.'</td><td>'.$actionIcon.'</td></tr>';
        }else{
            // file not existing in current version
            $pFData = VMS_Thickbox_CompareVersions_FindIt('filename', $filename, $prevFiles);
            if($pFData !== NULL){
                // file exists in previous version
                $actionIcon = '<img src="'.$CONFIG->imageUrl.'/delete.png" alt="Delete" />';
            }

            $tableContent .= '<tr><td>'.$filename.'</td><td>'.$actionIcon.'</td></tr>';
        }
    }






    if($tableContent == ''){
        $tableContent = '<tr><td>No changes in files between versions '.$version.' and '.$prevVersion.'</td>';
    }

    $table .= $tableContent.'</tbody></table>';
    return $table;




}


function VMS_Thickbox_CompareVersions_FindIt($keyname, $value, $arr){
    foreach($arr as $k=>$v){
        if($v[$keyname] == $value) return $v;
    }
    return NULL;
}




